<?php
namespace App\Services\Admin;

use Illuminate\Support\Facades\DB;

class AdService extends BaseService
{
    /**
     * 广告
     */

    /**
     * 广告列表
     * @param array $pages【start起始页，limit每页记录数】
     */
    public function getIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbAdList)->where('del', 1);
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'asc')
            ->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有了');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'uid' => $v['uid'],
                'thumb' => file_exists($v['thumb']) ? $v['thumb'] : '',
                'link' => $v['link'],
                'total' => $v['total'],
                'num' => $v['num'],
                'isrun' => $v['isrun'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $res);
    }

    /**
     * 已展示列表
     * @param array $pages【start起始页，limit每页记录数】
     */
    public function usedList($pages, $id)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db =  $this->dbConn($this->dbAdUsed);
        if ($id) {
            $db = $db->where('ad_id', $id);
        }
        $total = $db->count();
        $res =  $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $k => $v) {
            $v = (array) $v;
            $adOne = (array) $this->dbConn($this->dbAdList)
                ->where('id', $v['ad_id'])
                ->first();
            $thumb = '';
            if (!empty($adOne) && file_exists($adOne['thumb'])) {
                $thumb = $adOne['thumb'];
            }
            $arys[] = [
                'id' => $v['id'],
                'ad_id' => $v['ad_id'],
                'thumb' => $thumb,
                'pub_uid' => $v['pub_uid'],
                'plat_uid' => $v['plat_uid'],
                'h5code' => $v['h5code'],
                'addtime' => $v['addtime'],
                'usetime' => $v['usetime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 添加
     * @param array $pas
     */
    public function add($pas)
    {
        $pas['addtime'] = $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbAdList)->insertGetId($pas);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
//        try {
//        } catch (\Exception $e) {
//            echo $e->getMessage();
//            return array('code' => -1, 'msg' => 'error');
//        }
    }

    /**
     * 修改
     * @param int $id
     * @param array $pas
     */
    public function  modify($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbAdList)
            ->where('del', 1)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbAdList)->where('id', $id)->update($pas);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 设置启用
     * @param int $id
     * return array
     */
    public function setRun($id)
    {
        //看看广告在不在
        $adOne = (array) $this->dbConn($this->dbAdList)
            ->where('id', $id)
            ->first();
        if (empty($adOne)) {
            return $this->toback(-2, '没有记录');
        }
        $isrun = $adOne['isrun']==1 ? 2 : 1;
        $up = $this->dbConn($this->dbAdList)
            ->where('id', $id)
            ->update(['isrun' => $isrun, 'runtime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
//        try {
//            //广告修改
//        } catch (\Exception $e) {
//            echo $e->getMessage();
//            return array('code' => -1, 'msg' => 'error');
//        }
    }

    /**
     * 设置删除
     * @param int $id
     * return array
     */
    public function setDel($id)
    {
        //看看广告位在不在
        $adOne = (array) $this->dbConn($this->dbAdList)
            ->where('id', $id)
            ->first();
        if (empty($adOne)) {
            return $this->toback(-2, '没有记录');
        }
        $up = $this->dbConn($this->dbAdList)
            ->where('id', $id)
            ->update(['del' => 2, 'deltime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
//        try {
//            //广告修改
//        } catch (\Exception $e) {
//            echo $e->getMessage();
//            return array('code' => -1, 'msg' => 'error');
//        }
    }
}